/*
 * Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.changil.nowonchangilchurch.page;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.changil.nowonchangilchurch.R;
import com.changil.nowonchangilchurch.common.SleepTask;
import com.changil.nowonchangilchurch.common.SleepTask.SleepTaskListener;

public abstract class BaseNetworkFragment extends BaseFragment {
	private View mLoadingView = null;
	private View mRetryView = null;
	private boolean mIsAlreadyVisibled = false;
	private boolean mIsInit = false;
	private boolean mIsForceDataLoad = false;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		mLoadingView = inflater.inflate(R.layout.loading_layer, null);
		mLoadingView.setVisibility(View.GONE);
		mLoadingView.setPadding(0, 0, 0, getActivity().getResources().getDimensionPixelSize(R.dimen.dp92));
		mRetryView = inflater.inflate(R.layout.retry_layer, null);
		mRetryView.setVisibility(View.GONE);
		mRetryView.setPadding(0, 0, 0, getActivity().getResources().getDimensionPixelSize(R.dimen.dp92));
		((ViewGroup)getContentView()).addView(mLoadingView);
		((ViewGroup)getContentView()).addView(mRetryView);
		mIsInit = true;
		if(mIsForceDataLoad) {
			setLoadingView(true);
			loadData();
		}
		return getContentView();
	}
	
	protected abstract void loadData(); 
	
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if(isVisibleToUser) {
			if(!mIsAlreadyVisibled && mIsInit && !mIsForceDataLoad) {
				mIsAlreadyVisibled = true;
				setLoadingView(true);
				loadData();
			}
		}
	}
	
	public void setForceDataLoad(boolean isForce) {
		mIsForceDataLoad = isForce;
	}
	
	protected void setLoadingView(boolean isShow) {
		if(isShow) {
			mLoadingView.setVisibility(View.VISIBLE);
		} else {
			mLoadingView.setVisibility(View.GONE);
		}
	}
	
	protected void setRetryView(boolean isShow) {
		if(isShow) {
			mRetryView.setVisibility(View.VISIBLE);
			getContentView().findViewById(R.id.retry_layer_button).setOnClickListener(mOnClickListener);
		} else {
			mRetryView.setVisibility(View.GONE);
		}
	}
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			setRetryView(false);
			setLoadingView(true);
			SleepTask task = SleepTask.newInstance();
			task.setSleepTaskListener(new SleepTaskListener() {
				
				@Override
				public void onSleepTaskCompleted() {
					loadData();
				}
			});
			task.execute(500);
		}
	};
}